<template>
  <div class="container">
    <Breadcrumb :items="['问卷管理', '问卷分析']" />
    <a-card class="general-card" title="选择图表类型">
      <a-divider style="margin-top: 0" />
      <a-table
        row-key="id"
        :loading="loading"
        :pagination="pagination"
        :data="questionData.questions"
        :bordered="false"
        @page-change="onPageChange"
      >
        <template #columns>
          <a-table-column title="问题题干" data-index="stem" />
          <a-table-column title="操作" data-index="operations">
            <template #cell="record">
              <a-button
                type="text"
                status="success"
                size="small"
                @click="bingtuButton(record)"
              >
                柄图
              </a-button>
              <a-button
                type="text"
                status="warning"
                size="small"
                @click="tiaoxingtuButton(record)"
              >
                条形图
              </a-button>
              <a-button
                type="text"
                status="warning"
                size="small"
                @click="zhexiantuButton(record)"
              >
                折线图
              </a-button>
              <a-button
                type="text"
                status="danger"
                size="small"
                @click="ciyuntuButton(record)"
              >
                词云图
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { useSurveyStore } from '@/store';
  import { computed, ref, reactive } from 'vue';
  import { useI18n } from 'vue-i18n';
  import {
    deleteQuestion,
    previewsurvey,
    surveyList,
    PolicyRecord,
    PolicyParams,
    deleteSurvey,
    surveyStatics,
  } from '@/api/list';

  import useLoading from '@/hooks/loading';

  import { Pagination } from '@/types/global';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  import { Message } from '@arco-design/web-vue';
  import router from '@/router';
  import questionDataSurveyStore from '@/store/modules/data-screen';
  import modifySurvey from './modifySurvey.vue';
  import previewSurvey from './preview/previewSurvey.vue';
  // eslint-disable-next-line no-redeclare

  const questionData = questionDataSurveyStore();

  // 解析不了

  // 选择类型
  const ind = ref(0);
  function addind() {
    ind.value += 1;
  }
  const bingtuButton = (record: any) => {
    questionData.questions[ind.value].chartType = 'pie';
    ind.value += 1;
    console.log(record.record);
  };

  const tiaoxingtuButton = (record: any) => {
    questionData.questions[ind.value].chartType = 'bar';
    ind.value += 1;
    console.log(record.record);
  };

  const zhexiantuButton = (record: any) => {
    questionData.questions[ind.value].chartType = 'line';
    ind.value += 1;
    console.log(record.record);
  };

  const ciyuntuButton = (record: any) => {
    questionData.questions[ind.value].chartType = 'wordcloud';
    ind.value += 1;
    console.log(record.record);
  };

  // 查询往后传的东西
  const toBack = () => {
    return {
      survey_title: '',
      survey_state: '',
    };
  };

  //
  // const previewVisible = ref(false);
  // const dialogTableValue = ref();
  // 展示的东西
  const modifyVisible = ref(false);
  const dialogTableValue = ref({});
  const generateFormModel = () => {
    return {
      number: '',
      name: '',
      description: '',
      startTime: '',
      endTime: '',
      status: '',
    };
  };
  const { loading, setLoading } = useLoading(true);
  const { t } = useI18n();
  const renderData = ref<PolicyRecord[]>([]);
  const formModel = ref(generateFormModel());
  const basePagination: Pagination = {
    page: 1,
    limit: 20,
  };
  const pagination = reactive({
    ...basePagination,
  });

  const fetchData = async (
    params = {
      page: 1,
      limit: 20,
      name: formModel.value.name,
      isDeleted: '0',
    }
  ) => {
    setLoading(true);
    try {
      const { data } = await surveyList(params);
      renderData.value = data.list;
      pagination.page = params.page;
      pagination.total = data.total;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const search = () => {
    fetchData({
      ...basePagination,
      ...formModel.value,
    } as unknown as PolicyParams);
  };
  const onPageChange = (page: number) => {
    fetchData({ ...basePagination, page });
  };

  fetchData();
  const reset = () => {
    formModel.value = generateFormModel();
  };
  const modifyQ = (data) => {
    modifyVisible.value = true;
    console.log(data.record);
    dialogTableValue.value = JSON.parse(JSON.stringify(data.record));
    console.log(`zheli${dialogTableValue.value}`);
  };

  const deleteS = async (record) => {
    // console.log(record.id);
    await deleteSurvey([record.record.id]);
    fetchData();
  };

  const analys = async (record) => {
    // const param = {id:record.record.id};
    const { data } = await surveyStatics(record.record.id);
    // console.log("data.questions.stem")
    // console.log(data.questions[0].stem)
    // console.log("data.questions.stem")
    // eslint-disable-next-line prefer-destructuring
    questionData.questions = data.questions;
    // console.log("questionData.questions[0].stem")
    // console.log(questionData.questions[0].stem)
    // console.log("questionData.questions[0].stem")
    router.push({
      name: 'ScreenMultiDimensionDataAnalysis',
    });
  };
  // 预览那个按钮
  // 先写发请求吧
  const preview = (record) => {
    // const data1=ref();
    // const previewToSurvey = async (params = { id: record.record.id }) => {
    //   // setLoading(true);
    //   try {
    //     dialogTableValue.value = await previewsurvey(params);
    //     console.log('这里这里这里');
    //     console.log(dialogTableValue.value);
    //   } finally {
    //     setLoading(false);
    //   }
    // };
    // previewToSurvey();
    useSurveyStore.$id = record.record.id;
    useSurveyStore().from = 1;
    router.push({ name: 'AnserQuestion' });
  };
  const copyId = ref('a');
  const act = (record) => {
    copyId.value = `访问地址：http://localhost:3000 \n
     您的问卷邀请码为：${record.record.id}`;
  };
  const release = (record) => {
    console.log(record.record);
    const input = document.createElement('input'); // js创建一个input输入框
    input.value = copyId.value; // 将需要复制的文本赋值到创建的input输入框中，this.ruleForm.url这个是我要复制的内容
    useSurveyStore.$id = record.record.id;
    useSurveyStore().from = 0;
    router.push({
      name: 'AnserQuestion',
    });
    Message.success('复制成功，正在跳转...');
    document.body.appendChild(input); // 将输入框暂时创建到实例里面
    input.select(); // 选中输入框中的内容
    document.execCommand('Copy'); // 执行复制操作
    document.body.removeChild(input); // 最后删除实例中临时创建的input输入框，完成复制操作
  };
</script>

<script lang="ts">
  export default {
    name: 'SearchTable',
  };
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 20px 20px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
